﻿using Common.EF.Controllers;
using Common.Utility;
using Common.ViewModels;
using Stylet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows;
using AWSV2.ViewModels;

namespace AWSV2.ViewModels
{
    public class WeighingRecordShellViewModel : Screen
    {
        public event EventHandler Closed;

        /// <summary>
        /// 控件初始化之后赋值 --阿吉 2025年1月17日20点44分
        /// </summary>
        private Window _windowIns;

        public void WindowLoaded(object sender,RoutedEventArgs e)
        {
            _windowIns = (Window)sender;
        }


        //用于打开子窗口vm
        private IWindowManager _windowManager;

        public WeightListViewModel wl1 { get; set; }
        public WeightListViewModel wl0 { get; set; }

        public WeightAccountViewModel wa { get; set; }
        public WeightTotalViewModel wt { get; set; }
        public WeighPlanListViewModel WeighPlan { get; set; }

        public WeighingRecordShellViewModel(IWindowManager windowManager)
        {
            _windowManager = windowManager;

            var timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(1.5)
            };
            timer.Tick += (_, __) =>
            {
                timer.Stop();
                Prepare();
            };
            timer.Start();

        }

        protected override void OnClose()
        {
            base.OnClose();
            Closed?.Invoke(this, new EventArgs());
        }

        public void ActiveWindow()
        {
            _windowIns?.Activate();
        }

        private void Prepare()
        {

            wl1 ??= new WeightListViewModel(_windowManager, true);
            wl1.RefreshPage();
        }

        public void TabSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!AJDatabaseService.Connected)
            {
                return;
            }
            if (e.OriginalSource is TabControl && ((TabControl)sender).SelectedItem is TabItem tabItem)
            {
                var tag = tabItem.Tag.ToString();
                switch (tag)
                {
                    case nameof(wl1):
                        wl1 ??= new WeightListViewModel(_windowManager, true);
                        wl1.RefreshPage();
                        break;
                    case nameof(wl0):
                        wl0 ??= new WeightListViewModel(_windowManager, false);
                        wl1.RefreshPage();
                        break;
                    case nameof(wa):
                        wa ??= new WeightAccountViewModel(_windowManager);
                        wa.RefreshPage();
                        break;
                    case nameof(wt):
                        wt ??= new WeightTotalViewModel(_windowManager);
                        wt.RefreshPage();
                        break;
                    case nameof(WeighPlan):
                        WeighPlan ??= new WeighPlanListViewModel(_windowManager);
                        WeighPlan.RefreshPage();
                        break;
                    default:
                        break;
                }

            }
            e.Handled = true;
        }
    }
}
